package in.muton.edushare.pages.coursepages;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.auth.roles.DeptRoleCheck;
import in.muton.edushare.auth.roles.UnivRoleCheck;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.deptpages.DeptPage;
import in.muton.edushare.pages.univ.UnivPage;
import in.muton.edushare.processing.objects.CourseDataHandler;
import in.muton.edushare.processing.objects.DeptDataHandler;
import in.muton.edushare.url.EntityPageParams;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.CayenneModel;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.dao.CourseInstancesDAO;
import com.edushare.common.dao.CoursesDAO;
import com.edushare.common.exceptions.EduShareRuntimeException;
import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.AcademicYearModel;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Courses;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;

public class CourseUnavailable extends EduShareBasePage {

	public CourseUnavailable(final PageParameters params) {
		getCourses(params);
		add(new Label("courseTitle", getCourse().getObject().getCourseName()));
		CoursesDAO cDAO = new CoursesDAO(getSessionORMInterface());
		Departments d = cDAO.getParentDept(getCourse().getObject());
		Link<Void> l = new BookmarkablePageLink<Void>("deptLink",
				DeptPage.class, EntityPageParams
						.getParams(new Model<Departments>(d)));

		l.add(new Label("deptName", d.getDeptName()));
		add(l);

		l = new BookmarkablePageLink<Void>("univLink", UnivPage.class,
				EntityPageParams.getParams(new DeptDataHandler(
						new Model<Departments>(d)).getOwner()));
		l.add(new Label("univName", new DeptDataHandler(new Model<Departments>(
				d)).getOwner().getObject().getUniversityName()));

		add(l);

		WebMarkupContainer wmc = new WebMarkupContainer("prevSems");
		add(wmc);
		wmc.setVisible(getCourse().getObject().getCourseInstances().size() > 0);
		CayenneList<CourseInstances> cL = new CayenneList<CourseInstances>(
				course.getObject().getCourseInstances());
		wmc.add(new ListView<IModel<CourseInstances>>("inst", cL) {
			private static final long serialVersionUID = 9212827747630491013L;

			@Override
			protected void populateItem(ListItem<IModel<CourseInstances>> arg0) {
				Link<Void> l = new BookmarkablePageLink<Void>("instLink",
						CoursePage.class, EntityPageParams.getParams(
								new Model<Courses>(arg0.getModelObject()
										.getObject().getCourse()), arg0
										.getModelObject().getObject()
										.getCourseInstanceId().toString()));
				
				String instName = arg0.getModelObject().getObject().getCourse().getCourseName();
				AcademicYearModel a = arg0.getModelObject().getObject().getSemester();
				if(a != null)
					instName += ", "+ a.getSemesterName();
				else
					instName += ", Unknown Semester"; 
				Integer acadYear = arg0.getModelObject().getObject().getInstanceYear();
				if(acadYear != null)
					instName += " - "+ acadYear;
				else
					instName += "; Unknown year";
				l.add(new Label("instName",instName));
				arg0.add(l);
			}
		});

		WebMarkupContainer wmc2 = new WebMarkupContainer("newInstPanel");
		add(wmc2);
		Departments dept = new CourseDataHandler(course, 0).getOwner().getObject();
		Universities univ = new CourseDataHandler(course, 0).getOwnerUniv().getObject();
		
		wmc2.setVisible(DeptRoleCheck.isPrimaryAdmin(dept, getLoggedInUser())||
				UnivRoleCheck.isAdmin(univ, getLoggedInUser()));
		Form<Void> f = new Form<Void>("form") {
			@Override
			public void onSubmit() {
				new CourseInstancesDAO(getSessionORMInterface())
						.newStandaloneInst(getCourse().getObject());
				setResponsePage(CoursePage.class, params);
			}
			
			@Override
			public boolean isVisible(){
				Users u = EduShareSession.get().getLoggedInUser();				
				Departments dept = new CoursesDAO(getSessionORMInterface()).getParentDept(getCourse().getObject());
				DeptDataHandler ddh = new DeptDataHandler(new Model<Departments>(dept));
				if(ddh.getPrimaryAdmins().contains(u) || ddh.getUnivAdmins().contains(u))
					return true;
				return false;
			}

			private static final long serialVersionUID = 1L;
		};

		wmc2.add(f);

	}

	private void getCourses(PageParameters params) {
		String id = params.getString("1");
		if (StringUtil.isValid(id)) {
			Courses course = new CoursesDAO(getSessionORMInterface())
					.getCourseById(Integer.parseInt(id));
			if (course != null) {
				setCourse(new CayenneModel<Courses>(course));
				return;
			}
		}
		throw new EduShareRuntimeException("Unable to find courses");
	}

	@Override
	public String getPageTitle() {
		return getLoggedInUser().getCompleteUserName() + " | "
		+ getCourse().getObject().getCourseName()
		+ " | Course Unavailable | "
		+ ApplicationConstants.APPLICATION_NAME;		
	}

	@Override
	public String getUserRole() {
		return "";
	}

	public void setCourse(IModel<Courses> course) {
		this.course = course;
	}

	public IModel<Courses> getCourse() {
		return course;
	}

	private IModel<Courses> course;
}
